/**
 * @Description:
 * @author 若琳
 * @date 2021/6/29
*/


const express = require('express');
const LoginController = express.Router();
const UAParser = require("ua-parser-js");
const request = require("request");

//网上找的  淘宝IP地理库也可以
let BaiDuMap = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=IP&co=&resource_id=6006&t=1555898284898&ie=utf8&oe=utf8&format=json&tn=baidu"
//数据集合
const ResultJson = require("../../../../../../../../ruoling-model/src/com/ruoling/model/SystemModel/ResultJson.js").V("v1.0.0");

const UserAgentUtils = require("../../../../../../../../ruoling-common/src/com/ruoling/common/utils/useragentutils.js");

//业务
const LoginService = require("../../../../../../../../ruoling-system/src/com/ruoling/system/service/v1/system/service.login.js");


LoginController.get("/login",function(Request,response){

    response.type(".html");
    response.render("admin/system/login/login.html");

});



/**
 * 登录
 */
LoginController.post("/v1/api/login",function(Request,response){

    try{
        let {sys_loginname,sys_loginpwd,captchaImage} = Request.body;

        //IP
        let ip = UserAgentUtils.GetClientIp(Request);
        let GetUserAgent = UserAgentUtils.GetUserAgent(Request);
        //浏览器
        let browser = GetUserAgent.browser;
        //操作系统
        let OS = GetUserAgent.OS;
        let addressapi = BaiDuMap.replace("IP","124.228.221.177");
        if(sys_loginname && sys_loginpwd && captchaImage){

            // request(addressapi,function(err,res,body){
            //     if (!err && response.statusCode == 200) {
            //         let dataMap = JSON.parse(body);
            //         let address = "";
            //         if(dataMap.data.length == 0){
            //             address = "暂无"
            //         }else{
            //             address = dataMap.data[0].location.substring(0,dataMap.data[0].location.indexOf(" "));
            //         }
            //         LoginService.UserLogin(sys_loginname,sys_loginpwd,captchaImage,Request.session,Request,ip,browser,OS,address,(err,data)=>{
            //             if(err){
            //                 response.send(ResultJson.GUBError());
            //                 return;
            //             }
            //             if(data.code == 0){
            //
            //                 Request.session.UserSession = data.data.user;
            //                 Request.session.MenuSession = data.data.menu;
            //                 response.send(ResultJson.MegOk(data.msg));
            //                 return;
            //             }else{
            //                 response.send(ResultJson.Error(data.msg));
            //                 return;
            //             }
            //
            //         });
            //
            //     }else{
            //         response.send(ResultJson.Error("地理信息接口报错"));
            //         return;
            //     }
            // });
            let address = "暂无"
            LoginService.UserLogin(sys_loginname,sys_loginpwd,captchaImage,Request.session,Request,ip,browser,OS,address,(err,data)=>{
                    if(err){
                        response.send(ResultJson.GUBError());
                        return;
                    }
                    if(data.code == 0){

                        Request.session.UserSession = data.data.user;
                        Request.session.MenuSession = data.data.menu;
                        response.send(ResultJson.MegOk(data.msg));
                        return;
                    }else{
                        response.send(ResultJson.Error(data.msg));
                        return;
                    }

                });

        }else{
            response.send(ResultJson.ParamsError());
            return;
        }
    }catch (e){

        response.send(ResultJson.SystemError());
        return;
    }
});



/**
 * 退出登录
 */
LoginController.get("/v1/api/logout",function(Request,response){

    LoginService.LogoutService(Request.sessionID,(err,data)=>{

        Request.session.MenuSession = null;
        Request.session.UserSession = null;
        Request.session.captchaImage = null;
        response.redirect("/admin/login");
        return;
    })

});


module.exports = LoginController;